package com.bweab.controll.userstbcontrol;

import com.alibaba.druid.support.json.JSONParser;
import com.alibaba.druid.support.json.JSONUtils;
import com.bweab.pojo.UsersTb;
import com.bweab.service.api.DepositTbServiceInterface;
import com.bweab.service.api.UsersTbServiceInterface;
import com.bweab.service.impl.UsersTbServiceInterfaceImpl;
import com.bweab.util.JSONResult;
import com.bweab.util.JSONStats;
import com.bweab.util.WebUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;


@WebServlet("/login")
public class Login extends HttpServlet {
    UsersTbServiceInterface userService = new UsersTbServiceInterfaceImpl();//调用user服务端接口
    JSONResult result =new JSONResult();

    //测试前后端通信的
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");

        String userpassword = req.getParameter("userpassword");
        System.out.println();
        System.out.println("用户名是:" + username + "\n用户密码是" + userpassword);

        //调用服务层  获取密码
        UsersTb user = userService.getUserInfo(username);//获取对象
        //用户名判断
        if(user==null){
                result.setStatusCode(JSONStats.USERNAME_NULL.getStatusCode());
                result.setMessage(JSONStats.USERNAME_NULL.getMessage());
        }else {

            if(!user.getUserName().equals(username)){
                //设置用户名错误的状态码
                result.setStatusCode(JSONStats.USERNAME_ERROR.getStatusCode());
                //设置用户名错误的信息
                result.setMessage(JSONStats.USERNAME_ERROR.getMessage());

            }else if(!user.getUserPassword().equals(userpassword)){//密码判断
                //设置用户密码错误的状态码
                result.setStatusCode(JSONStats.LOGIN_PASSWORD_ERROR.getStatusCode());
                //设置用户密码错误的信息
                result.setMessage(JSONStats.LOGIN_PASSWORD_ERROR.getMessage());

            }else{//登录成功
                //定义用户登录成功的状态码
                result.setStatusCode(JSONStats.SUCCESS.getStatusCode());
                //定义用户登录成功的消息
                result.setMessage(JSONStats.SUCCESS.getMessage());
                user.setUserPassword("");
                result.setData(user);

//                //同时设置session
//                //获取session
//                HttpSession session = req.getSession();
//
//
//                //设置session
//                //用户名
//                session.setAttribute("username",user.getUserName());
//                //用户性别
//                session.setAttribute("userSex",user.getUserSex());
//                //用户学号
//                session.setAttribute("userXh",user.getUserXh());
//                //用户手机号
//                session.setAttribute("userPhone",user.getUserName());
//                //用户身份
//                session.setAttribute("userStatus",user.getUserStatus());
            }
        }
        System.out.println("控制登录层");
        WebUtil.writeJson(resp,result);
        //可以通过session 设置session username 用户身份的状态的判断




//        //定义状态码
//        result.setStatusCode(JSONStats.SUCCESS.getStatusCode());
//        //定义信息
//        result.setMessage(JSONStats.SUCCESS.getMessage());
//        //定义响应对象体数据
//        result.setData(user);
//
//
//        System.out.println("控制登录层");
//
//        WebUtil.writeJson(resp,result);



    }
}
